package com.jinsilu.freight.ui.activity;

import android.text.TextUtils;
import android.view.View;

import com.jinsilu.freight.R;
import com.jinsilu.freight.common.MyVBActivity;
import com.jinsilu.freight.databinding.ActivityWaybillDetailsBinding;
import com.jinsilu.freight.helper.ShowImageHelper;
import com.jinsilu.freight.http.response.WaybillBean;

import java.math.BigDecimal;

/**
 * 运单->已完成->查看详情（运单详情）
 *
 * @author yangyang
 * @time 2020-11-17 10:37
 */
public class WaybillDetailsActivity extends MyVBActivity<ActivityWaybillDetailsBinding> {

    private WaybillBean mWaybill;

    @Override
    protected void initView() {

        setOnClickListener(R.id.tv_btn_contact_customer_service);

    }

    @Override
    protected void initData() {

        mWaybill = (WaybillBean) getIntent().getSerializableExtra("waybill");
        if (null == mWaybill) return;
        // TODO: 2021-02-05 是否需要调用订单详情接口
//        binding.tvWaybillPayStatus.setText("已支付");
        binding.tvWaybillNumber.setText(getNotNullString(mWaybill.getWaybillNumber(), ""));
        binding.tvWaybillAddress.setText(getNotNullString(mWaybill.getLineName(), ""));
        binding.tvWaybillGoodsName.setText(getNotNullString(mWaybill.getGoodsName(), ""));
        binding.tvWaybillUnitPrice.setText(String.format("%s元/吨", getNotNullString(mWaybill.getSettlePrice(), "0.00")));
        binding.tvWaybillAllowTons.setText(String.format("%s吨", getNotNullString(mWaybill.getLossWeight(), "0.00")));
        binding.tvWaybillLoadingWeight.setText(String.format("%s吨", getNotNullString(mWaybill.getUploadWeight(), "0.00")));
        binding.tvWaybillUnloadingWeight.setText(String.format("%s吨", getNotNullString(mWaybill.getUnloadWeight(), "0.00")));
        binding.tvWaybillGrabOrderTime.setText(String.format("%s", getNotNullString(mWaybill.getGrabTime(), "")));
        binding.tvWaybillLoadingTime.setText(String.format("%s", getNotNullString(mWaybill.getUploadTime(), "")));
        binding.tvWaybillUnloadingTime.setText(String.format("%s", getNotNullString(mWaybill.getUnloadTime(), "")));
        binding.tvWaybillSettleTime.setText(String.format("%s", getNotNullString(mWaybill.getPayTime(), "")));

        // update 2022年5月31日
        binding.tvDeductionForLossOfTons.setText(String.format("%s", getNotNullString(mWaybill.getLossWeightMoney(), "0.00")));
        binding.tvDeductionForOthers.setText(String.format("%s", getNotNullString(mWaybill.getOtherMoney(), "0.00")));
        binding.tvFreightPayable.setText(String.format("%s", getNotNullString(mWaybill.getCashMoney(), "0.00")));
        binding.tvDeductionOfIous.setText(String.format("%s", getNotNullString(mWaybill.getDeductionMoney(), "0.00")));
        if(mWaybill.getDeductionMoney() == null){
            binding.tvActuallyPaidShipping.setText(String.format("%s", new BigDecimal(mWaybill.getCashMoney())));
        }else {
            binding.tvActuallyPaidShipping.setText(String.format("%s", new BigDecimal(mWaybill.getCashMoney()).subtract(new BigDecimal(mWaybill.getDeductionMoney()))));
        }

        // end update 2022年5月31日

        binding.tvWaybillTotalShipping.setText(String.format("%s", getNotNullString(mWaybill.getSettleMoney(), "")));
        // W_cut_fee   +  W_allow_loss_price
//        BigDecimal totalFee = new BigDecimal(getNotNullString(mWaybill.getRisePrice(), "0.00"))
//                .add(new BigDecimal(getNotNullString(mWaybill.getWOilFee(), "0.00")))
//                .add(new BigDecimal(getNotNullString(mWaybill.getAirFee(), "0.00")))
//                .add(new BigDecimal(getNotNullString(mWaybill.getWDeposit(), "0.00")))
//                .add(new BigDecimal(getNotNullString(mWaybill.getWPrepaidAmount(), "0.00")))
//                .add(new BigDecimal(getNotNullString(mWaybill.getWOtherCutFee(), "0.00")))
//                .add(new BigDecimal(getNotNullString(mWaybill.getWConstructionFee(), "0.00")));
        binding.tvWaybillTotalDeduction.setText(String.format("%s", mWaybill.getSettlePrice()));
        binding.tvWaybillActual.setText(String.format("%s", getNotNullString(mWaybill.getCashMoney(), "")));

        ShowImageHelper.showNormalRound(this, getNotNullString(mWaybill.getUploadImgUrl(), ""), binding.ivWaybillLoadingPicture, dp2px(6));
        ShowImageHelper.showNormalRound(this, getNotNullString(mWaybill.getUnloadImgUrl(), ""), binding.ivWaybillUnloadingPicture, dp2px(6));

        binding.ivWaybillLoadingPicture.setOnClickListener(v -> {
            if (!TextUtils.isEmpty(mWaybill.getUploadImgUrl()))
                ShowImageHelper.showImageView(this, mWaybill.getUploadImgUrl());
        });

        binding.ivWaybillUnloadingPicture.setOnClickListener(v -> {
            if (!TextUtils.isEmpty(mWaybill.getUnloadImgUrl()))
                ShowImageHelper.showImageView(this, mWaybill.getUnloadImgUrl());
        });

    }

    private String getNotNullString(String wOrderPrice, String emptyPlaceholder) {
        return TextUtils.isEmpty(wOrderPrice) ? emptyPlaceholder : wOrderPrice;
    }

    @Override
    public void onClick(View v) {
//        if (v == binding.tvBtnContactCustomerService) {
//            PhoneUtils.dial("19809432233");
//        }
    }
}
